<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Docker镜像加速器检测工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            padding: 20px;
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .container {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .panel {
            flex: 1;
            background-color: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .panel h2 {
            margin-top: 0;
            border-bottom: 2px solid #eee;
            padding-bottom: 10px;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .result-item {
            border: 1px solid #eee;
            border-radius: 4px;
            padding: 15px;
            margin-bottom: 10px;
            background-color: #fafafa;
        }
        .result-item.valid {
            border-left: 4px solid #28a745;
        }
        .result-item.invalid {
            border-left: 4px solid #dc3545;
        }
        .result-url {
            font-weight: bold;
            font-size: 18px;
            margin-bottom: 5px;
        }
        .result-status {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 3px;
            font-weight: bold;
        }
        .status-valid {
            background-color: #d4edda;
            color: #155724;
        }
        .status-invalid {
            background-color: #f8d7da;
            color: #721c24;
        }
        .result-time {
            color: #666;
            margin: 5px 0;
        }
        .result-error {
            color: #dc3545;
            margin: 5px 0;
            font-size: 14px;
        }
        .loading {
            text-align: center;
            padding: 20px;
        }
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #007bff;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .timestamp {
            color: #666;
            font-size: 14px;
            text-align: right;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="header">
        <h1>Docker镜像加速器检测工具</h1>
        <p>检测常用Docker镜像加速器的有效性和响应速度</p>
    </div>

    <div class="container">
        <div class="panel">
            <h2>检测控制</h2>
            <button id="testButton" onclick="runTest()">开始检测</button>
            <p>点击按钮开始检测所有镜像加速器的有效性</p>
        </div>

        <div class="panel">
            <h2>镜像加速器列表</h2>
            <div id="mirrorsList">
                <p>加载中...</p>
            </div>
        </div>
    </div>

    <div class="panel">
        <h2>检测结果</h2>
        <div id="results">
            <p>点击"开始检测"按钮运行检测</p>
        </div>
    </div>

    <script>
        // 页面加载完成后获取镜像加速器列表
        window.onload = function() {
            fetchMirrors();
        };

        // 获取镜像加速器列表
        function fetchMirrors() {
            fetch('/mirrors')
                .then(response => response.json())
                .then(data => {
                    const mirrorsList = document.getElementById('mirrorsList');
                    mirrorsList.innerHTML = '<ul>' + data.map(mirror => `<li>${mirror}</li>`).join('') + '</ul>';
                })
                .catch(error => {
                    console.error('Error fetching mirrors:', error);
                    document.getElementById('mirrorsList').innerHTML = '<p>加载失败</p>';
                });
        }

        // 运行检测
        function runTest() {
            const testButton = document.getElementById('testButton');
            const resultsDiv = document.getElementById('results');
            
            // 禁用按钮并显示加载状态
            testButton.disabled = true;
            testButton.textContent = '检测中...';
            resultsDiv.innerHTML = `
                <div class="loading">
                    <div class="spinner"></div>
                    <p>正在检测镜像加速器，请稍候...</p>
                </div>
            `;

            // 发起检测请求
            fetch('/test')
                .then(response => response.json())
                .then(data => {
                    displayResults(data);
                })
                .catch(error => {
                    console.error('Error:', error);
                    resultsDiv.innerHTML = `<p style="color: red;">检测失败: ${error.message || error}</p>`;
                })
                .finally(() => {
                    // 恢复按钮状态
                    testButton.disabled = false;
                    testButton.textContent = '开始检测';
                });
        }

        // 显示检测结果
        function displayResults(data) {
            const resultsDiv = document.getElementById('results');
            const timestamp = new Date(data.timestamp * 1000).toLocaleString('zh-CN');
            
            if (!data.results || data.results.length === 0) {
                resultsDiv.innerHTML = '<p>未获取到检测结果</p>';
                return;
            }

            let html = `<div class="timestamp">检测时间: ${timestamp}</div>`;
            
            data.results.forEach(result => {
                const statusClass = result.is_valid ? 'valid' : 'invalid';
                const statusText = result.is_valid ? '有效' : '无效';
                const statusStyle = result.is_valid ? 'status-valid' : 'status-invalid';
                
                html += `
                    <div class="result-item ${statusClass}">
                        <div class="result-url">${result.mirror_url}</div>
                        <div>
                            状态: 
                            <span class="result-status ${statusStyle}">${statusText}</span>
                        </div>
                        <div class="result-time">响应时间: ${result.response_time !== null ? result.response_time + 's' : 'N/A'}</div>
                        ${result.error_message ? `<div class="result-error">错误信息: ${result.error_message}</div>` : ''}
                    </div>
                `;
            });
            
            resultsDiv.innerHTML = html;
        }
    </script>
</body>
</html>